package com.wlit.ui;

import com.wlit.tianping.IDataListener;
import com.wlit.tianping.TianpingConfig;
import com.wlit.tianping.TianpingConstant;
import com.wlit.tianping.TianpingService;
import gnu.io.CommPortIdentifier;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

public class TianpingDialog extends JDialog implements IDataListener {
    private JComboBox<String> portList; // 串口列表
    private JLabel portLabel; // 串口标签
    private JButton openButton, closeButton, testButton, saveButton, pureButton,zeroButton; // 按钮
    private JTextArea logTextArea; // 文本区域显示日志

    public TianpingDialog(JFrame owner) {
        super(owner, "天平配置", true); // 设置模态对话框
        initComponents();
        pack();
        setLocationRelativeTo(owner);
    }

    private void initComponents() {
        String comName = TianpingConfig.getInstance().getComName();
        setLayout(new BorderLayout());
        // 创建串口标签
        portLabel = new JLabel("选择串口：");

        // 创建串口列表
        portList = new JComboBox<>();

        java.util.List<String> thePorts = new java.util.ArrayList<>();
        Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers();
        while (portIdentifiers.hasMoreElements()) {
            CommPortIdentifier nextElement = portIdentifiers.nextElement();
            if (nextElement.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                String name = nextElement.getName();
                thePorts.add(name);
            }
        }
        //让默认选中的串口为第一个位置
        if (comName != null && !comName.isEmpty()) {
            portList.addItem(comName);
            thePorts.remove(comName);
        }
        for(String port:thePorts){
            portList.addItem(port);
        }
        // 创建按钮
        openButton = new JButton("打开串口");
        closeButton = new JButton("关闭串口");
        testButton = new JButton("测试串口");
        saveButton = new JButton("保存配置");
        pureButton = new JButton("去皮");
        zeroButton = new JButton("置零");

        // 创建日志文本区域
        logTextArea = new JTextArea(10, 30);
        JScrollPane scrollPane = new JScrollPane(logTextArea);

        // 创建一个面板用于放置串口标签和串口列表
        JPanel portPanel = new JPanel(new FlowLayout());
        portPanel.add(portLabel);
        portPanel.add(portList);

        // 添加组件到对话框
        add(portPanel, BorderLayout.NORTH);
        add(new JPanel(new FlowLayout()) {{
            add(openButton);
            add(closeButton);
            add(testButton);
            add(saveButton);
            add(pureButton);
            add(zeroButton);
        }}, BorderLayout.CENTER);
        add(scrollPane, BorderLayout.SOUTH);

        // 为按钮添加事件监听器
        openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String portName = (String) portList.getSelectedItem();
                TianpingService.getInstance().openComm(portName);
                if (TianpingService.getInstance().isOpen()) {
                    log("串口打开成功");
                    openButton.setEnabled(false);
                    closeButton.setEnabled(true);
                } else {
                    log("串口打开失败");
                    openButton.setEnabled(true);
                    closeButton.setEnabled(false);
                }
                // 打开串口的逻辑
            }
        });

        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TianpingService.getInstance().closeComm();
                openButton.setEnabled(true);
                closeButton.setEnabled(false);
                log("串口关闭");
                // 关闭串口的逻辑
            }
        });

        testButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TianpingService.getInstance().sendData(TianpingConstant.WEIGHT_CMD);
            }
        });

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 保存配置的逻辑
                String portName = (String)portList.getSelectedItem();
                TianpingConfig.getInstance().save(portName);
                JOptionPane.showMessageDialog(TianpingDialog.this, "串口"+portName+"配置已保存");
            }
        });

        pureButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                TianpingService.getInstance().sendData("001T\r\n");
            }
        });

        zeroButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                TianpingService.getInstance().sendData("001Z\r\n");
            }
        });

        if (TianpingService.getInstance().isOpen()){
            openButton.setEnabled(false);
            closeButton.setEnabled(true);
            log("串口已打开");
        }else{
            openButton.setEnabled(true);
            closeButton.setEnabled(false);
            log("串口未打开");
        }
    }

    // 打印日志到文本区域的方法
    private void log(String message) {
        logTextArea.append(message + "\n");
    }


    @Override
    public void onReceiver(String data) {
        log("称重结果:"+data+"克");
    }
}